import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:kq_flutter_core_widget/utils/kq_screen_util.dart';
import 'package:get/get.dart';
import 'package:kq_flutter_core_widget/utils/ex/list_ex.dart';
import 'package:kq_flutter_core_widget/utils/kq_multi_click_util.dart';
import '../../resources/kq_theme_colors.dart';
import '../../resources/l10n/kq_intl.dart';
import 'package:kq_flutter_core_widget/utils/text_filed_utils.dart';
import 'package:kq_flutter_core_widget/widgets/button/kq_ink_well.dart';
import 'package:kq_flutter_widgets/theme/kq_theme_manager.dart';

import '../divider/kq_divider.dart';
import '../textFiled/kq_textfiled.dart';

/// 通用输入对话框
///
/// @author 周卓
///
class KqInputDialog extends AlertDialog {
  /// 标题
  final String? titleString;

  final String? subTitle;
  final Color? subTitleColor;
  final double? subTitleFontSize;

  /// 默认值
  final String? defValue;

  /// 提示文字，默认"请输入"
  final String? placeHolder;

  /// 当行模式点击键盘换行键是否自动提交
  final bool autoSubmitWhenReturn;

  /// 是否多行，默认false
  final bool multiline;

  /// 是否输入密码
  final bool password;

  /// 最大行数，不传不限制
  final int? maxLines;

  /// 左边按钮文字，默认"取消"
  final String? leftBtnText;

  /// 中间按钮文字，默认null,不传则不显示
  final String? middleBtnText;

  /// 左边按钮文字，默认"确定"
  final String? rightBtnText;

  /// 输入类型，默认[TextInputType.text]
  final TextInputType? inputType;

  /// 输入框下方自定义内容
  final List<Widget>? customWidgetsBelowInput;

  /// 输入内容限制
  final List<TextInputFormatter>? inputFormatters;

  /// 输入框文字对齐方式
  final TextAlign? inputTextAlign;

  /// 是否显示清空按钮
  final bool enableClearButton;

  /// 输入回调
  final Function(String value)? onValueChanged;

  /// 左边按钮回调
  final Function(String value)? onLeftBtnTap;

  /// 中间按钮回调
  final Function(String value)? onMiddleBtnTap;

  /// 右边按钮回调
  final Function(String value)? onRightBtnTap;

  /// 输入框内容控制器
  final TextEditingController editController;

  /// 最大字符数，默认不限制
  final int? maxCharCount;

  KqInputDialog(
      {Key? key,
      this.defValue,
      this.titleString,
      this.multiline = false,
      this.autoSubmitWhenReturn = false,
      this.maxLines,
      this.inputFormatters,
      this.placeHolder,
      this.inputTextAlign,
      this.inputType,
      this.leftBtnText,
      this.middleBtnText,
      this.rightBtnText,
      this.enableClearButton = false,
      this.password = false,
      this.onValueChanged,
      this.onLeftBtnTap,
      required this.editController,
      this.customWidgetsBelowInput,
      this.onMiddleBtnTap,
      this.onRightBtnTap,
      this.maxCharCount,
      this.subTitle,
      this.subTitleColor,
      this.subTitleFontSize})
      : super(key: key) {
    if (defValue != null) {
      TextFiledUtil.setValue(editController, defValue!);
    }
  }

  @override
  Widget build(BuildContext context) {
    bool isLangScape = context.orientation == Orientation.landscape;

    final List<Widget> children = <Widget>[];

    /// 上边距
    children.add(SizedBox(
      width: 1.r,
      height: 24.r,
    ));

    if (titleString != null) {
      children.add(Container(
        padding: EdgeInsets.symmetric(horizontal: 16.r),
        alignment: Alignment.center,
        child: Text(
          titleString!,
          style: TextStyle(
              fontSize: 16.sp,
              color:
                  KqThemeManager.instance.getConfig().dialogConfig.titleColor ??
                      KqThemeColors.text26,
              fontWeight: FontWeight.w600),
        ),
      ));
    }

    if (subTitle != null) {
      if (titleString != null) {
        children.add(SizedBox(
          width: 1.r,
          height: 12.r,
        ));
      }

      children.add(Container(
        padding: EdgeInsets.symmetric(horizontal: 16.r),
        alignment: Alignment.center,
        child: Text(
          subTitle!,
          style: TextStyle(
            fontSize: subTitleFontSize ?? 12.sp,
            color: subTitleColor ?? KqThemeColors.text59,
          ),
        ),
      ));
    }

    if (titleString != null || subTitle != null) {
      children.add(SizedBox(
        width: 1.r,
        height: titleString != null && subTitle != null ? 12.r : 24.r,
      ));
    }

    /// 输入框
    children.add(Container(
      margin: EdgeInsets.symmetric(horizontal: 16.r),
      padding: EdgeInsets.symmetric(horizontal: 10.r, vertical: 12.r),
      decoration: BoxDecoration(
          border: Border.all(
              width: 1,
              color:
                  KqThemeManager.instance.getConfig().commonConfig.lineColor ??
                      KqThemeColors.lineColor),
          color: KqThemeColors.bgTransparent),
      child: KqTextFiled(
        multiline: multiline,
        useParentHeight: false,
        placeHolder: placeHolder,
        autofocus: true,
        controller: editController,
        maxLines: maxLines,
        password: password,
        maxCharCount: maxCharCount,
        inputFormatters: inputFormatters,
        enableClearButton: enableClearButton,
        onChanged: (value) {
          // editController.text = value;
          if (onValueChanged != null) {
            onValueChanged!(value);
          }
        },
        onSubmitted: (value) {
          if (onRightBtnTap != null && autoSubmitWhenReturn) {
            onRightBtnTap!(value);
          }
        },
        defValue: defValue,
        inputType: inputType,
        textInputAction:
            multiline ? TextInputAction.newline : TextInputAction.done,
        textAlign: inputTextAlign ?? TextAlign.start,
      ),
    ));

    /// 自定义控件
    if (customWidgetsBelowInput.isNotNullOrEmpty) {
      for (var value in customWidgetsBelowInput!) {
        children.add(value);
      }
    }

    /// 下边距
    children.add(SizedBox(
      width: 1.r,
      height: 24.r,
    ));

    /// 按钮
    children.add(const KqDivider());

    children.add(Row(
      children: [
        Expanded(
            child: KqInkWell(
          onTap: () {
            if (KqMultiClickUtil.isMultiClick()) {
              return;
            }
            if (onLeftBtnTap != null) {
              onLeftBtnTap!(editController.text);
            }
          },
          child: Container(
            alignment: Alignment.center,
            width: double.infinity,
            height: 48.r,
            child: Text(
              leftBtnText ?? KqIntl.currentResource.cancel,
              style: TextStyle(
                  fontSize: 16.sp,
                  color: KqThemeManager.instance
                          .getConfig()
                          .dialogConfig
                          .cancelBtnColor ??
                      KqThemeColors.text8C),
            ),
          ),
        )),
        KqVerticalDivider(
          height: 48.r,
        ),
        Visibility(
            visible: middleBtnText != null,
            child: Expanded(
              child: KqInkWell(
                onTap: () {
                  if (KqMultiClickUtil.isMultiClick()) {
                    return;
                  }
                  if (onMiddleBtnTap != null) {
                    onMiddleBtnTap!(editController.text);
                  }
                },
                child: Container(
                  alignment: Alignment.center,
                  width: double.infinity,
                  height: 48.r,
                  child: Text(
                    middleBtnText ?? '',
                    style: TextStyle(
                        fontSize: 16.sp,
                        color: KqThemeManager.instance
                                .getConfig()
                                .dialogConfig
                                .middleBtnColor ??
                            KqThemeColors.text8C),
                  ),
                ),
              ),
            )),
        Visibility(
          visible: middleBtnText != null,
          child: KqVerticalDivider(
            height: 48.r,
          ),
        ),
        Expanded(
            child: KqInkWell(
          onTap: () {
            if (KqMultiClickUtil.isMultiClick()) {
              return;
            }
            if (onRightBtnTap != null) {
              onRightBtnTap!(editController.text);
            }
          },
          child: Container(
            alignment: Alignment.center,
            width: double.infinity,
            height: 48.r,
            child: Text(
              rightBtnText ?? KqIntl.currentResource.confirm,
              style: TextStyle(
                  fontSize: 16.sp,
                  color: KqThemeManager.getDialogConfig().mainBtnColor ??
                      KqThemeColors.textLightBlue),
            ),
          ),
        ))
      ],
    ));

    Widget dialogChild = Column(
      mainAxisSize: MainAxisSize.min,
      crossAxisAlignment: CrossAxisAlignment.stretch,
      children: children,
    );
    var padding = MediaQuery.of(context).viewInsets.bottom;
    return UnconstrainedBox(
        child: Padding(
      padding: EdgeInsets.only(bottom: padding),
      child: SizedBox(
          width: 320.r,
          child: Material(
              shape: RoundedRectangleBorder(
                  borderRadius: BorderRadius.all(Radius.circular(
                      KqThemeManager.instance.getConfig().dialogConfig.radius ??
                          8.r))),
              color: KqThemeManager.getDialogConfig().bgColor ?? KqThemeColors.bgWhite,
              child: LimitedBox(
                maxHeight: isLangScape ? 350.r : 400.r,
                child: ClipRRect(
                  borderRadius: BorderRadius.all(Radius.circular(
                      KqThemeManager.instance.getConfig().dialogConfig.radius ??
                          8.r)),
                  child: dialogChild,
                ),
              ))),
    ));
  }
}
